package com.example.ffmpegclipvideomodule.activity;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;


import com.blankj.utilcode.constant.PermissionConstants;
import com.blankj.utilcode.util.PermissionUtils;
import com.example.ffmpegclipvideomodule.R;
import com.example.ffmpegclipvideomodule.base.BaseActivity;
import com.example.ffmpegclipvideomodule.databinding.ActivityTestBinding;
import com.example.ffmpegclipvideomodule.listener.VideoChangeState;
import com.example.ffmpegclipvideomodule.util.VideoTrimmerUtil;

import java.util.List;

public class TestActivity extends BaseActivity<ActivityTestBinding> {
    private VideoTrimmerUtil videoTrimmerUtil;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_test;
    }

    @Override
    protected void initData() {
        super.initData();

        videoTrimmerUtil = new VideoTrimmerUtil();
        PermissionUtils.permission(PermissionConstants.STORAGE).callback(new PermissionUtils.SingleCallback() {
            @Override
            public void callback(boolean isAllGranted, @NonNull List<String> granted, @NonNull List<String> deniedForever, @NonNull List<String> denied) {
                Log.e("PPS","   isAllGranted  " + isAllGranted);

            }
        }).request();




        mDataBinding.mBtn.setOnClickListener(v->{

            /*String oldPath = "/storage/emulated/0/DCIM/Camera/VID_20200924_145502.mp4";
            //String oldPath = "/storage/emulated/0/eps168/RecordingScreen/16日09月2020年_14时12分58秒.mp4";
            String newPath = "/storage/emulated/0/cutVideo.mp4";
            String newFBLPath = "/storage/emulated/0/newFBLVideo.mp4";
            //String cutVideoCmd = "ffmpeg -ss %d -t %d -i %s -acodec copy -vcodec copy %s";
            //裁剪视频
            videoTrimmerUtil.clipVideo(oldPath, newPath, "0", "61", new VideoChangeState() {
                @Override
                public void onStartChange() {
                    Log.e("PPS","开始裁剪");
                    runOnUiThread(()->{
                        toast("开始裁剪");
                    });

                }

                @Override
                public void onChanging(int progress) {
                    Log.e("PPS","正在裁剪 "  + progress);
                }

                @Override
                public void onFinishChange(String url) {
                    Log.e("PPS","裁剪完成");
                    runOnUiThread(()->{
                        toast("裁剪完成");
                        videoTrimmerUtil.changeVideoFBL(oldPath, newFBLPath, new VideoChangeState() {
                            @Override
                            public void onStartChange() {
                                runOnUiThread(()->{
                                    toast("开始修改分辨率");
                                });
                            }

                            @Override
                            public void onChanging(int progress) {

                            }

                            @Override
                            public void onFinishChange(String url) {
                                runOnUiThread(()->{
                                    toast("修改分辨率完成" + url);
                                });
                            }
                        });
                    });

                }


            });*/
            //修改分辨率
            //VideoTrimmerUtil.changeVideoFBL(oldPath,newFBLPath);

            Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
            intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"video/*");
            startActivityForResult(intent,0x123);
        });


    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.e("PPS","  resultCode " + resultCode +"   requestCode  " + requestCode);
        if ( requestCode == 0x123){
            Uri uri = data.getData();
            Cursor cursor = getContentResolver().query(uri, null, null, null,null);
            if (cursor != null && cursor.moveToFirst()) {
                String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
                cursor.close();
                Log.e("PPS"," path = " + path);
                Intent intent = new Intent(mContext,ClipVideoActivity.class);
                intent.putExtra("path",path);
                startActivityForResult(intent,0x124);
            }

        }

        if (resultCode == 0x124){
            String path = data.getStringExtra("path");
            Log.e("PPS"," 裁剪完成的视频路径 path = " + path);
        }
    }
}